package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.Response.PageResult;
import com.qk.dto.RolePageDTO;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> getRoles(RolePageDTO rolePageDTO) {
        rolePageDTO.checkPageParam();
        PageHelper.startPage(rolePageDTO.getPage(),rolePageDTO.getPageSize());
        List<Role> roles = roleMapper.selectPageRoles(rolePageDTO);
        PageResult<Role> pageResult = new PageResult<>();
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        pageResult.setTotal(rolePageInfo.getTotal());
        pageResult.setRows(rolePageInfo.getList());
        return pageResult;
    }

    @Override
    public void addRole(Role role) {
        //参数校验
        if (Objects.isNull(role)) {
            throw new RuntimeException("参数不能为空");
        }
        role.setUpdateTime(LocalDateTime.now());
        role.setCreateTime(LocalDateTime.now());
        roleMapper.addRole(role);

    }

    @Override
    public void deleteRoleById(Integer id) {
        //参数校验
        if (Objects.isNull(id)) {
            throw new RuntimeException("参数不能为空");
        }
        roleMapper.deleteRoleById(id);
    }

    @Override
    public Role selectRoleById(Integer id) {
        if (Objects.isNull(id)) {
            throw new RuntimeException("参数不能为空");
        }
       return roleMapper.selectRoleById(id);
    }

    @Override
    public void updateRole(Role role) {
        //参数校验
        if (Objects.isNull(role)) {
            throw new RuntimeException("参数不能为空");
        }
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public List<Role> selectAllRole() {
        List<Role> roles = roleMapper.selectAllRole();
        return  roles;
    }
}
